package com.enjoy.game.xcard;

import java.io.InputStream;
import java.util.ArrayList;

import org.andengine.entity.IEntity;
import org.andengine.entity.scene.Scene;
import org.andengine.extension.tmx.TMXLayer;
import org.andengine.extension.tmx.TMXLoader;
import org.andengine.extension.tmx.TMXTiledMap;
import org.andengine.extension.tmx.util.exception.TMXLoadException;
import org.andengine.input.touch.TouchEvent;
import org.andengine.opengl.texture.TextureOptions;
import org.andengine.ui.activity.BaseGameActivity;

import com.enjoy.game.xcard.action.MovementController;
import com.enjoy.game.xcard.sprite.Battle;
import com.enjoy.game.xcard.sprite.CardInfo;
import com.enjoy.game.xcard.sprite.CellLayout;
import com.enjoy.game.xcard.sprite.DeckArea;
import com.enjoy.game.xcard.sprite.DeckBuilder;
import com.enjoy.game.xcard.sprite.DragEventDispatcher;
import com.enjoy.game.xcard.sprite.IExEntity;
import com.enjoy.game.xcard.sprite.XCard;
import com.enjoy.game.xcard.sprite.IDragDispatcherHolder;

import android.content.Context;

public class Table extends Scene implements IDragDispatcherHolder {

	
	private TMXTiledMap mTMXTiledMap;
	BaseGameActivity mContext;
	DragEventDispatcher mDragEventDispatcher;
	CellLayout mCardSelectLayout;
	DeckArea mDeckArea;
	Battle mBattle;
	
	public Table(BaseGameActivity context, final InputStream istream){
		TMXLoader tmxLoader = new TMXLoader(context.getAssets(), context.getTextureManager(),
				TextureOptions.BILINEAR_PREMULTIPLYALPHA, context.getVertexBufferObjectManager());
		
		try {
			mTMXTiledMap = tmxLoader.load(istream);
		} catch (TMXLoadException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}  

		TMXLayer tmxBgLayer = mTMXTiledMap.getTMXLayers().get(0);
		if("backgroud".equalsIgnoreCase(tmxBgLayer.getName())){
			attachChild(tmxBgLayer);
		}
		
		
		TMXLayer gridLayer = mTMXTiledMap.getTMXLayers().get(1);
		attachChild(gridLayer);
		
		mCardSelectLayout = new CellLayout(5, 5, XCard.CARD_WIDTH, XCard.CARD_HEIGHT + 5,4, 3);
		mDeckArea = new DeckArea(5, 600, XCard.CARD_WIDTH, XCard.CARD_HEIGHT + 5, 5, 1);
		mBattle = new Battle(100, 130, XCard.CARD_WIDTH, XCard.CARD_HEIGHT + 5, 3, 3);
		attachChild(mCardSelectLayout);
		attachChild(mDeckArea);
		attachChild(mBattle);
		mDeckArea.setScale(0.8f);
		mBattle.setScale(0.8f);
	}

	@Override
	public void registerDragDispatcher(int x, int y, IEntity entity){
		
	}
	
	public CellLayout getCardSelectedCellLayout( ){
		return mCardSelectLayout;
	}
	
	
	public DeckArea getDeckArea( ){
		return mDeckArea;
	}
	
	public Battle getBattle(){
		return mBattle;
	}
	
	
	@Override
	public boolean onSceneTouchEvent(TouchEvent pSceneTouchEvent) {
		// TODO Auto-generated method stub
		return super.onSceneTouchEvent(pSceneTouchEvent);
	}

	@Override
	public void attachChild(IEntity pEntity) throws IllegalStateException {
		super.attachChild(pEntity);
		if(pEntity instanceof IExEntity){
			((IExEntity) pEntity).onAttachScene(this);
		}
	}
	
	
	
	
	
}
